//
//  SPOrderViewController.m
//  panzer
//
//  Created by 还带大道 on 2018/10/21.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPOrderViewController.h"
#import "SPCommon.h"
#import "SPOrderListTableViewCell.h"
#import "SPWattingSendDetailViewController.h"
#import "SPAfterSaleDetailViewController.h"
#import "SPOrderListModel.h"


@interface SPOrderViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *title_dfh;
@property (weak, nonatomic) IBOutlet UILabel *line_dfh;
@property (weak, nonatomic) IBOutlet UILabel *title_sh;
@property (weak, nonatomic) IBOutlet UILabel *line_sh;
@property (assign, nonatomic) SJOrderType type;
@property (nonatomic,assign) NSInteger pageIndex;
@property (nonatomic,strong) NSMutableArray<SPOrderListModel *> *list_order;

@end

@implementation SPOrderViewController

- (instancetype)initWithType:(SJOrderType)type
{
    if(self =[super init]){
        _type = type;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"订单处理";
    self.pageIndex = 1;
    _list_order = [NSMutableArray array];
    [self setupTableView];
    
    switch (_type) {
        case SJOrderType_DFH:
            [self dfh_onClick];
            break;
        case SJOrderType_SH:
            [self sh_onClick];
            break;
    }
}



- (void)setupTableView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *headerV = [UIView new];
    headerV.backgroundColor = RZ_COLOR(245, 245, 245);
    headerV.height = 15;
    self.tableView.tableHeaderView = headerV;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPOrderListTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([SPOrderListTableViewCell class])];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(fetchOrderData)];
}


#pragma mark--

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;
    switch (_type) {
        case SJOrderType_DFH:
            count = self.list_order.count;
            break;
        case SJOrderType_SH:
            count = self.list_order.count;
            break;
    }
    return count;;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPOrderListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SPOrderListTableViewCell class])];
    cell.model = self.list_order[indexPath.row].goods_list.firstObject;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPOrderListModel *model = self.list_order[indexPath.row];
    if(model.status == 1){
        SPWattingSendDetailViewController *ws = [[SPWattingSendDetailViewController alloc] initWithOrder_id:model.order_id];
        [self.navigationController pushViewController:ws animated:YES];
    }else{
        SPAfterSaleDetailViewController *as = [[SPAfterSaleDetailViewController alloc] initWithOrder_id:model.order_id];
        [self.navigationController pushViewController:as animated:YES];
    }
}




//待发货
- (IBAction)dfh_onClick{

    _type = SJOrderType_DFH;
    [self setupSelectedWithOrderType:_type];
    self.pageIndex = 1;
    [self.list_order removeAllObjects];
    [self fetchOrderData];
}

//售后
- (IBAction)sh_onClick{
    
    _type = SJOrderType_SH;
    [self setupSelectedWithOrderType:_type];
    self.pageIndex = 1;
    [self.list_order removeAllObjects];
    [self fetchOrderData];
    
}


- (void)setupSelectedWithOrderType:(SJOrderType)type
{
    [self setupTitle:_title_dfh line:_line_dfh selected:type == SJOrderType_DFH];
    [self setupTitle:_title_sh line:_line_sh selected:type == SJOrderType_SH];
}


- (void)setupTitle:(UILabel *)title line:(UILabel *)line selected:(BOOL)selected
{
    title.font = [UIFont fontWithName:selected?@"PingFangSC-Semibold":@"PingFang-SC-Medium" size:15];
    line.hidden = !selected;
}



#pragma mark--订单数据
- (void)fetchOrderData
{
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=order_list&token=568e86c8630f7b44f29a23ba211d9f25
     参数 status 【默认1  1为待发货 4 退换货】 page 第几页 psize 每页多少条 默认5
     
     */
    
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    dict[@"ac"] = @"member";
    dict[@"op"] = @"member";
    dict[@"ta"] = @"order_list";
    dict[@"page"] = @(self.pageIndex);
    dict[@"psize"] = @(10);
    if(_type == SJOrderType_DFH){
        dict[@"status"] = @(1);
    }else{
       dict[@"status"] = @(4);
    }
    
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        [self.tableView.mj_footer endRefreshing];
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            NSMutableArray<SPOrderListModel *> *items = [SPOrderListModel mj_objectArrayWithKeyValuesArray:res[@"data"]];
            if(self.pageIndex == 1){
                [self.list_order removeAllObjects];
            }
            [self.list_order addObjectsFromArray:items];
            [self.tableView reloadData];
            self.pageIndex++;
            
        }else{
//            [WToast showWithText:res[@"resultMessage"]];
            [self.tableView reloadData];
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    }];
}


@end
